/*** Eclipse Class Decompiler plugin, copyright (c) 2012 Chao Chen (cnfree2000@hotmail.com) ***/
package com.qihoo.gamecenter.sdk.suspend.a.a.a;

import android.content.Context;
import android.os.Environment;
import android.os.StatFs;
import android.text.TextUtils;
import com.qihoo.gamecenter.sdk.suspend.b.c;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.Comparator;

public abstract class a {
    protected Context a;
    protected boolean b = false;
    protected String c = null;
    protected int d = 10;

    protected abstract String b();

    protected abstract int c();

    public a(Context paramContext) {
        this.a = paramContext;
        e();
    }

    public String d() {
        if (this.a == null)
            return null;
        if (!(TextUtils.isEmpty(this.c)))
            return this.c;
        File localFile = null;
        this.b = Environment.getExternalStorageState().equals("mounted");
        if (this.b) {
            localFile = Environment.getExternalStorageDirectory();
            this.c = localFile.toString() + "/" + "gameunion/.cache";
        }
        else {
            localFile = this.a.getCacheDir();
            this.c = localFile.toString();
        }
        String str = b();
        if (!(TextUtils.isEmpty(str)))
            this.c = this.c + "/" + str;
        localFile = new File(this.c);
        localFile.mkdirs();
        c.a(this.c);
        return this.c;
    }

    private long a()
  {
    if ((TextUtils.isEmpty(this.c)) || (this.a == null))
      return 0L;
    try
    {
      File localFile = null;
      if (this.b)
        localFile = Environment.getExternalStorageDirectory();
      else
        localFile = Environment.getDataDirectory();
      StatFs localStatFs = new StatFs(localFile.getPath());
      double d1 = localStatFs.getAvailableBlocks() * localStatFs.getBlockSize() / 1048576.0D;
      return ()d1;
    }
    catch (Exception localException)
    {
      localException.printStackTrace();
    }
    return 0L;
  }

    private String a(String paramString) {
        String[] arrayOfString = paramString.split("/");
        return arrayOfString[(arrayOfString.length - 1)] + ".tmp";
    }

    private String h(String paramString) {
        String[] arrayOfString = paramString.split("/");
        return arrayOfString[(arrayOfString.length - 1)];
    }

    public String b(String paramString) {
        if (TextUtils.isEmpty(paramString))
            return null;
        return c(h(paramString));
    }

    public String c(String paramString) {
        if (TextUtils.isEmpty(paramString))
            return null;
        try {
            String str = d() + "/" + paramString;
            File localFile = new File(str);
            if ((localFile == null) || (!(localFile.exists())))
                break label68;
            i(str);
            return str;
        } catch (Exception localException) {
            localException.printStackTrace();
        }
        label68: return null;
    }

    private void i(String paramString) {
        if (TextUtils.isEmpty(paramString))
            return;
        try {
            File localFile = new File(paramString);
            long l = System.currentTimeMillis();
            localFile.setLastModified(l);
        } catch (Exception localException) {
            localException.printStackTrace();
        }
    }

    public String d(String paramString) {
        if (TextUtils.isEmpty(paramString))
            return null;
        String str = d() + "/" + paramString + ".tmp";
        return str;
    }

    public String e(String paramString) {
        if (TextUtils.isEmpty(paramString))
            return null;
        String str = d() + "/" + a(paramString);
        return str;
    }

    public String f(String paramString) {
        if (TextUtils.isEmpty(paramString))
            return null;
        String str = h(paramString);
        return g(str);
    }

    public String g(String paramString) {
        if (TextUtils.isEmpty(paramString))
            return null;
        if (10L > a())
            return null;
        String str = d() + "/" + paramString;
        try {
            File localFile1 = new File(d(paramString));
            File localFile2 = new File(str);
            if (localFile2.exists())
                localFile2.delete();
            com.qihoo.gamecenter.sdk.suspend.b.e.b.a(localFile1, localFile2);
            localFile1.delete();
            i(str);
            j(d());
        } catch (IOException localIOException) {
            com.qihoo.gamecenter.sdk.suspend.b.b.a("LocalBaseFileCache", localIOException);
            return null;
        }
        return str;
    }

    public void e() {
        this.d = c();
        j(d());
    }

    private boolean j(String paramString) {
        if (TextUtils.isEmpty(paramString))
            return false;
        File localFile = new File(paramString);
        File[] arrayOfFile = localFile.listFiles();
        if ((arrayOfFile == null) || (arrayOfFile.length <= 0))
            return true;
        int i = 0;
        for (int j = 0; j < arrayOfFile.length; ++j)
            i = (int) (i + arrayOfFile[j].length());
        if ((i > this.d * 1048576) || (10L > a())) {
            j = (int) (0.4D * arrayOfFile.length + 1.0D);
            Arrays.sort(arrayOfFile, new a(null));
            int k = 0;
            for (int l = 0; (k <= j) && (l < arrayOfFile.length); ++l) {
                if (arrayOfFile[l] == null)
                    continue;
                com.qihoo.gamecenter.sdk.suspend.b.b.a("LocalBaseFileCache",
                        "removeCache delete=" + arrayOfFile[l].getName());
                arrayOfFile[l].delete();
                arrayOfFile[l] = null;
                ++k;
            }
            for (l = 0; l < arrayOfFile.length; ++l) {
                if (arrayOfFile[l] == null)
                    continue;
                if (arrayOfFile[l].length() > 0L)
                    continue;
                arrayOfFile[l].delete();
                arrayOfFile[l] = null;
            }
        }
        return (a() > this.d);
    }

    private class a implements Comparator {
        public int a(File paramFile1, File paramFile2) {
            if (paramFile1.lastModified() > paramFile2.lastModified())
                return 1;
            if (paramFile1.lastModified() == paramFile2.lastModified())
                return 0;
            return -1;
        }
    }
}